package com.jsgt.greekfire.ui.wallet

import android.content.Context
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.base.BaseActivity
import com.fhx.core.goToActivity
import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.App
import com.jsgt.greekfire.R
import com.jsgt.greekfire.ui.dialog.DialogUtils
import com.jsgt.greekfire.dao.model.IdentityBean
import com.jsgt.greekfire.dao.model.IdentityWallet
import com.jsgt.greekfire.dao.model.WalletBean
import com.jsgt.greekfire.dao.viewmodel.WalletViewModel
import com.jsgt.greekfire.ui.dialog.InputDialogUtil
import com.jsgt.greekfire.ui.me.backup.BackupActivity
import com.jsgt.greekfire.ui.wallet.export.ExportKeyActivity
import kotlinx.android.synthetic.main.activity_receive.*
import kotlinx.android.synthetic.main.activity_wallet_manager_import.*

class WalletManagerImportActivity : BaseActivity(), View.OnClickListener {
    private val walletViewModel: WalletViewModel by lazy {
        ViewModelProvider(this).get(WalletViewModel::class.java)
    }

    private var wallet: IdentityWallet? = null
    private var identity: IdentityBean? = null

    override fun needFitsSystemWindows(): Boolean {
        return false
    }


    override fun getLayoutId(): Int {
        return R.layout.activity_wallet_manager_import
    }

    override fun main() {
        setStatusBar(R.color.transparent, false, R.color.white)

        title = getString(R.string.manage)

        wallet = intent.getParcelableExtra("wallet")
        if (wallet == null) {
            finish()
        }

        txvWalletAddress.text = wallet!!.address

        walletViewModel.getIdentity(wallet!!.identityId)?.observe(this, Observer {
            identity = it
            if (it != null) {
                if (it.mnemonic.isNullOrEmpty()) {
                    rlBackup.visibility = View.GONE
                    vLine.visibility = View.GONE
                }
            } else {
                finish()
            }
        })

        walletViewModel.getWallet(wallet!!.id)?.observe(this, Observer {
            if (it != null) {
                txvWalletName.text = it.name
            }
        })

        txvRemove.setOnClickListener(this)
        rlBackup.setOnClickListener(this)
        rlKey.setOnClickListener(this)
        txvWalletAddress.setOnClickListener(this)
        rlExport.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.txvRemove -> {
                walletViewModel.deleteIdentity(wallet!!.identityId)
            }
            R.id.rlBackup -> {
                identity?.let {
                    DialogUtils.showInputPwdDialog(
                        this, it.password
                    ) {
                        goToActivity<BackupActivity> {
                            putExtra("identityId", wallet!!.identityId)
                        }
                    }
                }
            }
            R.id.rlKey -> {
                identity?.let {
                    DialogUtils.showInputPwdDialog(
                        this, it.password
                    ) {
                        goToActivity<ExportKeyActivity> {
                            putExtra("identityId", wallet!!.identityId)
                        }
                    }
                }
            }
            R.id.txvWalletAddress -> {
                if (wallet != null) {
                    val clipboardManager = App.context.getSystemService(Context.CLIPBOARD_SERVICE)
                            as android.text.ClipboardManager
                    clipboardManager.text = txvAddress?.text.toString().trim()
                    ToastUtils.show(resources.getString(R.string.copied))
                }
            }
            R.id.rlExport -> {
                InputDialogUtil.showInputDialog(
                    this,
                    getString(R.string.modify_wallet_name),
                    getString(R.string.modify_wallet_name_hint)
                ) {
                    if (wallet != null && it.isNotEmpty()) {
                        val name = WalletBean.Name(wallet!!.id.toString(), it)
                        walletViewModel.updateWalletName(name)
                    }
                }
            }
        }
    }
}